from typing import List, Optional
from datetime import datetime
from pydantic import BaseModel, Field

from app.schemas.user import UserProfile


class TeamBase(BaseModel):
    name: str = Field(..., min_length=1, max_length=100)
    description: Optional[str] = Field(None, max_length=1000)
    max_members: Optional[int] = Field(None, ge=2, le=10)
    is_recruiting: bool = True
    required_skills: Optional[List[str]] = None


class TeamCreate(TeamBase):
    competition_id: int


class TeamUpdate(BaseModel):
    name: Optional[str] = Field(None, min_length=1, max_length=100)
    description: Optional[str] = Field(None, max_length=1000)
    max_members: Optional[int] = Field(None, ge=2, le=10)
    is_recruiting: Optional[bool] = None
    required_skills: Optional[List[str]] = None


class TeamMemberBase(BaseModel):
    role: str
    status: str


class TeamMemberResponse(TeamMemberBase):
    id: int
    user_id: int
    team_id: int
    user: UserProfile
    joined_at: Optional[datetime]
    left_at: Optional[datetime]
    created_at: datetime

    class Config:
        from_attributes = True


class TeamList(TeamBase):
    id: int
    competition_id: int
    leader_id: int
    status: str
    current_members: int = 0
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class TeamResponse(TeamBase):
    id: int
    competition_id: int
    leader_id: int
    status: str
    leader: UserProfile
    members: List[TeamMemberResponse] = []
    current_members: int = 0
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class TeamInvitationBase(BaseModel):
    message: Optional[str] = Field(None, max_length=500)


class TeamInvitationCreate(TeamInvitationBase):
    user_id: int


class TeamInvitationResponse(TeamInvitationBase):
    id: int
    team_id: int
    user_id: int
    invited_by: int
    status: str
    user: UserProfile
    invited_by_user: UserProfile
    expires_at: datetime
    created_at: datetime

    class Config:
        from_attributes = True